package core
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	public class ScrollController
	{
		private var _topTrigger:Sprite;
		private var _bottomTrigger:Sprite;
		private var _canvas:Sprite;
		private var _view:Sprite;
		private var _timer:Timer;
		private var _up:Boolean;
		
		public function ScrollController(view:Sprite)
		{
			_view = view;
			super();
			init();
		}
		
		private function init():void
		{
			_canvas = _view["canvas"]
			_timer = new Timer(50);
			_timer.addEventListener(TimerEvent.TIMER, scroll);
			
			_topTrigger = createTriggerShape("topTrigger");
			_topTrigger.y = 50;
			
			_bottomTrigger = createTriggerShape("bottomTrigger");
			_bottomTrigger.y = _canvas.stage.stageHeight - _bottomTrigger.height;
		}
	
		private function createTriggerShape(_name:String):Sprite
		{
			var sp:Sprite = _view[_name];
			sp.addEventListener(MouseEvent.ROLL_OVER, onMouse);
			sp.addEventListener(MouseEvent.ROLL_OUT, onMouse);
			return sp;
		}
		
		private function onMouse(e:MouseEvent):void
		{
			switch(e.type)
			{
				case MouseEvent.ROLL_OVER :
					switch(e.currentTarget.name)
					{
						case "topTrigger" :
							_up = true;
							_timer.start();
						break;
							
						case "bottomTrigger" :
							_up = false;
							_timer.start();
						break;
					}
				break;
				
				case MouseEvent.ROLL_OUT :
					_timer.stop();
				break;
			}
		}
		
		private function scroll(e:TimerEvent):void
		{
			if(_up) _canvas.y +=6;
			else 
			{
				if(_canvas.y <= 0)
				{
					_canvas.y = 0;
					return;
				}
				_canvas.y -= 6;
			}
		}
		
		public function set offset(val:Number):void
		{
			_canvas.y = val;	
		}
		
		public function get offset():Number
		{
			return _canvas.y;
		}
	}
}